package com.limbo.showapp.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.limbo.showapp.R;
import com.limbo.showapp.base.BaseActivity;
import com.limbo.showapp.base.BaseBean;
import com.limbo.showapp.constant.AppConstant;
import com.limbo.showapp.presenter.LoginPresenter;
import com.limbo.showapp.service.RegisterService;
import com.limbo.showapp.util.SPUtils;
import com.limbo.showapp.util.ToastUtils;
import com.limbo.showapp.view.IHttpView;
import com.limbo.showapp.widget.OnMultiClickListener;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by wangqi on 2018/9/11.
 */

public class LoginActivity extends BaseActivity<IHttpView, LoginPresenter> implements IHttpView<BaseBean>, EasyPermissions.PermissionCallbacks {

    private Button bt_enter;
    private String password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        final EditText et_password = findViewById(R.id.et_password);
        bt_enter = findViewById(R.id.bt_enter);

        bt_enter.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                password = et_password.getText().toString();
                if (!TextUtils.isEmpty(password)) {
                    mPresenter.login(password);
                    switchButton(false);
                }
            }
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        //检查权限
        checkAppPermissions();
    }

    private void checkAppPermissions() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            checkIsLogin();
            return;
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            EasyPermissions.requestPermissions(
                    this,
                    "申请权限",
                    0,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE);
        }else{
            checkIsLogin();
        }
    }

    private void switchButton(boolean b) {
        bt_enter.setClickable(b);
        if (b) {
            bt_enter.setText("进入MiMi");
        } else
            bt_enter.setText("进入中...");
    }

    private void initConfig() {
        //开启Service 上传设备信息
        startService(new Intent(this, RegisterService.class));

        SPUtils.setString(this, AppConstant.SP.USER_PASSWORD, password);
    }

    @Override
    protected LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        MainActivity.OpenActivity(this);
        //初始化配置
        initConfig();
        ToastUtils.getInstanc(this).showToast("登录成功！");
        switchButton(true);
        finish();
    }

    @Override
    public void onError(String error) {
        switchButton(true);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        //权限通过
        checkIsLogin();
    }

    private void checkIsLogin() {
        if (!TextUtils.isEmpty(SPUtils.getString(this, AppConstant.SP.USER_PASSWORD))) {
            MainActivity.OpenActivity(this);
            finish();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        //权限拒绝
        new AppSettingsDialog
                .Builder(this)
                .setRationale("应用权限获取失败，无法正常使用，是否打开设置")
                .setPositiveButton("是")
                .setNegativeButton("否")
                .build()
                .show();
    }
}
